//重写了Date的toString
Date.prototype.toString = function() {

	var y = this.getFullYear();
	var m = this.getMonth() + 1;
	var d = this.getDate();
	var h = this.getHours();
	var mi = this.getMinutes();
	var s = this.getSeconds();
	if (m < 10) {
		m = '0' + m;
	}
	if (d < 10) {
		d = '0' + d;
	}
	var ret = y + '-' + m + '-' + d;
	if (h != '') {
		ret += ' ' + h + ':' + mi + ':' + s;
	}
	return ret;    
};
//重写了Date的格式化
Date.prototype.format = function(fmt){
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

dwr.engine._errorHandler = function(message, ex) {
	dwr.engine._debug("Error: " + ex.javaClassName + ", " + ex.message, true);
    if (!message)  return;
    Message.error(message);
};

$.ajaxSetup({
    error: function(jqXHR, textStatus, errorThrown){
		switch (jqXHR.status){  
            case(500):  
                Message.error("请求数据异常！");  
                break;  
            case(401):  
                Message.error("未登录！");  
                break;  
            case(403):  
                Message.error("无权限执行此操作！");  
                break;  
            case(408):  
                Message.error("请求超时！");  
                break;  
            default:  
                Message.error("请求数据异常！"); 
    	}  
	}
});
//用在输入框onkeyup=""中只许输入数字
function isInputNumber(obj){
	var reg4= '0123456789';
	var str = obj.value;
	var ret = '';
	var inum= str.length;
	for (i = 0; i < str.length; i++){ 
		var c = str.charAt(i);
		if( reg4.indexOf(c) == -1) {
			str = str.substr(0,i)+""+str.substr(i+1);
			i--;
			continue;				
		}
	}
	obj.value = str
}
/**
 * 如果限制输入框输入整形的话，请使用这个函数，isInputNumber这个函数命名有歧义
 * @param obj
 */
function isInputInteger(obj) {
	isInputNumber(obj);
}
function isInputFloat(obj) {
	var reg4= '0123456789.';
	var str = obj.value;
	for (var i = 0; i < str.length; i++){ 
		var c = str.charAt(i);
		if( reg4.indexOf(c) == -1) {
			str = str.substr(0,i)+""+str.substr(i+1);
			i--;
			continue;				
		}
	}
	obj.value = str;
}
function isValidFileName(v) {
	if(v == '') {
		return true;
	}
	return /^[^\\\/:*?<>""|]+$/.test(v);
}
/**
 * 功能：　复选框函数， 点击后选择（或取消选择）所有函数
 * 输入： checkboxAll 总控制复选框， checkbox 其他复选框
 * 输出：
 */
function checkAll(checkboxAll, checkboxName){
	var checkbox = document.getElementsByName(checkboxName);
	if(!checkboxAll || !checkbox)
	{
		return;
	}	
	if('checkbox' == checkbox.type)
	{
		checkbox.checked = checkboxAll.checked;
	}
	else
	{	
		for(var i = 0; i < checkbox.length; i++)
		{
			checkbox[i].checked = checkboxAll.checked;
		}
	}
}
/**
 * 功能：　复选框函数， 点击后反选
 * 输入： checkboxOthers 总控制复选框， checkbox 其他复选框
 * 输出：
 */
function checkOthers(checkboxOthers, checkboxName){
	var checkbox = document.getElementsByName(checkboxName);
	if(!checkboxOthers || !checkbox)
	{
		return;
	}	
	if('checkbox' == checkbox.type)
	{
		checkbox.checked = !checkbox.checked;
	}
	else
	{	
		for(var i = 0; i < checkbox.length; i++)
		{
			checkbox[i].checked = !checkbox[i].checked;
		}
	}
}
//弹出窗口（地址，宽，高）
function getWindow(url,tagname,wwidth,wheight)
{
	var sh = screen.height;
	var sw = screen.width;
	var wh = wheight;
	var ww = wwidth;
	var wt = (sh - wh) / 2;
	var wl = (sw - ww) / 2;
	window.open(url,tagname,"resizable=1,scrollbars=1,fullscreen=0,location=0,width="+ww+",height="+wh+",top="+wt+",left="+wl);
}
function notNull(v){
	return v == null ? '' : v;
}

function defaultIfEmpty(v, defaultValue) {
	return (v == null || v == '') ?  defaultValue : v;
}
//实现对前端字符串进行GET编码的函数
function appEncodeURL(s){
	var reg = /^[\u0391-\uFFE5]+$/;
    var urlLength = s.length;
    var newS = "";
    for(var i = 0; i < urlLength; i++) {
        if(reg.test(s.charAt(i))) {
            newS += escape(s.charAt(i));
        }
        else {
            newS += s.charAt(i);
        }
    }
	if(newS.indexOf('_app_encoding_tag_=')== -1){
		if(newS.indexOf('?') >= 0) {
			newS = newS + '&_app_encoding_tag_=1';
		}else{
			newS = newS + '?_app_encoding_tag_=1';
		}
	}
	return encodeURI(newS);
}
/**
 * 对字符串特殊字符进行处理，主要用于字符串拼接时单双引号的解析
 * @param {Object} str
 */
function encodeJavascript(str) {    
    if (str == null || str == '') {
		return str;
	}
	var s = '';
    for (var i = 0; i < str.length; i++) {
        switch (str.substr(i, 1)) {
            case "\'":
                s += "\\&#39;";
                break;
            case "\"":
                s += "\\&quot;";
                break;
            case "\n":
                s += "<br>";
                break;
            default:
                s += str.substr(i, 1);
                break;
        }
    }
    return s;
}
/**
 * 根据name取得values
 * */
function getValuesByName(elementName) {
	
	var elements = document.getElementsByName(elementName);
	var values = [];
	if (elements) {
		if (elements.length) {
			for (var i = 0; i < elements.length; i++) {
				values[i] = elements[i].value;
			}
		} else {
			values = elements.value;
		}
	}
	return values;
}

function getLineText(text, num) {
	var len = 16;
	if (num) {
		len = num;
	}
	var index = 0;
	if (getLength(text) > len) {
		for (var i = 1;i <= text.length; i++) {
			if (getLength(text.substr(0, i)) > len) {
				index = i - 1;
				break;
			}
		}
	}
	if (index != 0) {
		text = text.substring(0, index) + '...'; 
	}
	return text;
}
function isNumber(value) {

	for (var i = 0; i < value.length; i++) {
		if (isNaN(parseInt(value.substring(i,i+1), 10))) {
			return false;
		}
	}
	return true;
};
/** 计算字符串长度，考虑中英文 */
function getLength(value) {
	return value.replace(/[^\x00-\xff]/g,"**").length;
}